import {
  TransformControls,
  TransformControlsGizmo,
  TransformControlsPlane,
} from "three/examples/jsm/controls/TransformControls";
import Base from "./Base";
import {
  AxesHelper,
  GridHelper,
  Line,
  MOUSE,
  PerspectiveCamera,
  Raycaster,
  Scene,
  Vector2,
} from "three";
import { OrbitControls } from "three/examples/jsm/controls/OrbitControls";

export default class Control {
  public transformControl: TransformControls;
  public orbitControl: OrbitControls;
  private base: Base;
  private camera: PerspectiveCamera;
  private canvas: HTMLCanvasElement;
  private scene: Scene;
  private transing: boolean;
  constructor() {
    const base = Base.getInstance();
    this.base = base;

    this.camera = base.camera.perspectiveCamera;
    this.canvas = base.canvas;
    this.scene = base.scene;

    // 正在变换的标志位
    this.transing = false;
    this.transformControl = this.createTransformControl();
    this.initTransformControlEvent();
    this.scene.add(this.transformControl);
    this.orbitControl = this.createOrbitControl();
  }

  private createTransformControl() {
    const transformControls = new TransformControls(this.camera, this.canvas);

    // 初始射线发射器
    const raycaster = new Raycaster();
    const mouse = new Vector2();
    let x = 0;
    let y = 0;
    let width = 0;
    let height = 0;

    // 鼠标的坐标要实时监控
    this.canvas.addEventListener("mousemove", (event) => {
      x = event.offsetX;
      y = event.offsetY;
      width = this.canvas.offsetWidth;
      height = this.canvas.offsetHeight;
      mouse.x = (x / width) * 2 - 1;
      mouse.y = (-y * 2) / height + 1;
    });

    this.canvas.addEventListener("click", (event) => {
      if (this.transing) {
        this.transing = false;
        return false;
      }

      raycaster.setFromCamera(mouse, this.camera);

      //移出变换控制器本身,并不想要其,会干扰
      this.scene.remove(...this.base.noTransfromControlObjs);
      const intersections = raycaster.intersectObjects(this.scene.children);
      //使用完要给添加回去
      this.scene.add(...this.base.noTransfromControlObjs);

      const targetIntersection = intersections.filter((intersection) => {
        return (
          // 除了上面的移出添加方法,也可是使用过滤的方法,来过滤掉一些不需要的内容
          !(intersection.object instanceof TransformControlsPlane) &&
          !(intersection.object instanceof AxesHelper) &&
          !(intersection.object instanceof GridHelper) &&
          !(intersection.object instanceof Line) &&
          !(intersection.object.parent instanceof TransformControlsGizmo)
          //   !(intersection.object instanceof Line2) &&
        );
      });

      console.log(targetIntersection);

      if (intersections.length) {
        // console.log(intersection);

        const object = intersections[0].object;

        // console.log(object);
        this.transformControl.attach(object);
      }
    });
    return transformControls;
  }

  private initTransformControlEvent() {
    this.transformControl.addEventListener("mouseDown", (e) => {
      this.transing = true;
    });
  }

  public update() {
    this.orbitControl.update();
  }

  private createOrbitControl() {
    const controls = new OrbitControls(this.camera, this.canvas);
    controls.enableDamping = true;
    controls.enableZoom = true;
    // 取消鼠标控制器的鼠标左键旋转功能
    controls.mouseButtons = {
      LEFT: null as unknown as MOUSE,
      MIDDLE: MOUSE.DOLLY,
      RIGHT: MOUSE.ROTATE,
    };

    return controls;
  }
}
